package cn.gzsxt.music.controller;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.gzsxt.music.query.UserQueryObject;
import cn.gzsxt.music.service.UserService;
import cn.gzsxt.music.vo.MsgObj;

@Controller
@Scope("request")
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService service;
	
	@RequestMapping(value="upload",method=RequestMethod.POST)
	public String upload(@RequestParam("file") MultipartFile user_icon) {
		
		System.out.println("开始上传");
		System.out.println(user_icon);
		File destFile = new File("e:/upload");
		if (!destFile.exists()) {
			destFile.mkdir();
		}
		String fileName = UUID.randomUUID().toString().replace("-", "");
		String extension = FilenameUtils.getExtension(user_icon.getOriginalFilename());
		String newFileName = fileName + "."+extension;
		System.out.println("---------------");
		System.out.println(newFileName);
		System.out.println("---------------");
		File file = new File(destFile, newFileName);
		System.out.println(file);
		System.out.println("++++++++++++++++++++");
		return null;
	}
	
	@RequestMapping("/userList")
	@ResponseBody
	public Map<String, Object> userList(UserQueryObject qo) {
		Map<String, Object> result = service.selectUserListByQo(qo);
		return result;
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public MsgObj updateUser(@RequestParam Map<String, Object> params) {
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = service.checkByUName(user_name);
		MsgObj msgObj = new MsgObj();
		if (user.size() == 0) {
			int row = service.update(params);
			msgObj = new MsgObj(0, "修改用户失败");
			if (row == 1) {
				msgObj = new MsgObj(1, "修改用户成功");
			}
		}else {
			msgObj = new MsgObj(0, "该用户已存在");
		}
		return msgObj;
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	public MsgObj addUser(@RequestParam Map<String, Object> params) {
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = service.checkByUName(user_name);
		MsgObj msgObj = new MsgObj();
		if (user.size() == 0) {
			int row = service.insert(params);
			msgObj = new MsgObj(0, "添加用户失败");
			if (row == 1) {
				msgObj = new MsgObj(1, "添加用户成功");
			}
		}else {
			msgObj = new MsgObj(0, "该用户已存在");
		}
		
		return msgObj;
	}
	
	@RequestMapping("/deleteUserById")
	@ResponseBody
	public MsgObj deleteUserById(@RequestParam("user_ids[]") String[] user_ids) {
		int row = service.deleteUserById(user_ids);
		MsgObj msgObj = new MsgObj(0, "删除用户失败");
		if (row != 0) {
			msgObj = new MsgObj(1, "删除用户成功");
		}
		return msgObj;
	}
	
	@RequestMapping("userPage")
	public String userPage() {
		return "userPage";
	}
}
